﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.NetBar;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Common;
using Jzy.Core.Common.Basic;
using Jzy.Core.Utility;
using FortuneHeaven.Entity.NetBar;

namespace FortuneHeaven.Admin.Pages.NetBar
{
    public partial class List : PageList
    {
        private NetBarBO NetBar
        {
            get
            {
                return NetBarBO.Instance<NetBarBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["Name"]))
                {
                    txtName.Text = Request.QueryString["Name"];
                }
                BindView();
            }
            base.InitSelectID(hidSelectIDs);
            AddAttr();
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = AppConfig.PageSize;
            setting.OrderBy = "RigisterDate desc";
            if (txtName.Text.Trim() != "")
            {
                setting.Where = "NetbarName like @p0 or Contacts like @p0 or Telephone like @p0 or Address like @p0";
            }

            string prefix = GlobalVar.WebCurrPath + "List.aspx?Name=" + Server.UrlEncode(txtName.Text);
            List<NetBarInfo> list = this.NetBar.GetList(setting, "%" + txtName.Text + "%");
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            litPager.Text = HtmlPager.Render(setting, prefix, true);

        }

        private void AddAttr()
        {
            JSUtil.ShowForm(lnkAdd, "增加网吧", null, GlobalVar.WebCurrPath + "Edit.aspx", 400, 262);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkStartup, btnStartup);
            JSUtil.ClickBtn(lnkStop, btnStop);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                this.NetBar.Delete(StrUtil.SplitToStr(strSelectIDs));
            }
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 400, 262);

                HyperLink lnkRecharge = e.Item.FindControl("lnkRecharge") as HyperLink;

                int netbarID = ((e.Item as ListViewDataItem).DataItem as NetBarInfo).ID;

                string url = GlobalVar.WebCurrPath + "RechargeAdd.aspx?NetBarID=" + netbarID;

                JSUtil.ShowForm(lnkRecharge, "充值", null, url, 350, 201);

            }
        }

        protected void btnStartup_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "") ;
            {
                List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
                //this.NetBar.Startup(lstMemberIDs);
                this.NetBar.SetStatus(lstMemberIDs, (int)NetBarStatus.启用);

            }
            BindView();
        }
        protected void btnStop_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "") ;
            {
                List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);

                this.NetBar.SetStatus(lstMemberIDs, (int)NetBarStatus.停用);
            }
            // NetBar.Stop(lstMemberIDs);
            BindView();
        }
    }
}